package org.dromara.wmscloud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.wmscloud.domain.bo.BaseWorkshopBo;
import org.dromara.wmscloud.domain.vo.BaseWorkshopVo;
import org.dromara.wmscloud.domain.BaseWorkshop;
import org.dromara.wmscloud.mapper.BaseWorkshopMapper;
import org.dromara.wmscloud.service.IBaseWorkshopService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 车间Service业务层处理
 *
 * @author hou
 * @date 2025-04-02
 */
@RequiredArgsConstructor
@Service
public class BaseWorkshopServiceImpl implements IBaseWorkshopService {

    private final BaseWorkshopMapper baseMapper;

    /**
     * 查询车间
     *
     * @param workshopId 主键
     * @return 车间
     */
    @Override
    public BaseWorkshopVo queryById(Long workshopId){
        return baseMapper.selectVoById(workshopId);
    }

    /**
     * 分页查询车间列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 车间分页列表
     */
    @Override
    public TableDataInfo<BaseWorkshopVo> queryPageList(BaseWorkshopBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseWorkshop> lqw = buildQueryWrapper(bo);
        Page<BaseWorkshopVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的车间列表
     *
     * @param bo 查询条件
     * @return 车间列表
     */
    @Override
    public List<BaseWorkshopVo> queryList(BaseWorkshopBo bo) {
        LambdaQueryWrapper<BaseWorkshop> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseWorkshop> buildQueryWrapper(BaseWorkshopBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseWorkshop> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BaseWorkshop::getWorkshopId);
        lqw.eq(StringUtils.isNotBlank(bo.getWorkshopCode()), BaseWorkshop::getWorkshopCode, bo.getWorkshopCode());
        lqw.like(StringUtils.isNotBlank(bo.getWorkshopName()), BaseWorkshop::getWorkshopName, bo.getWorkshopName());
        return lqw;
    }

    /**
     * 新增车间
     *
     * @param bo 车间
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseWorkshopBo bo) {
        BaseWorkshop add = MapstructUtils.convert(bo, BaseWorkshop.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setWorkshopId(add.getWorkshopId());
//        }
        return flag;
    }

    /**
     * 修改车间
     *
     * @param bo 车间
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseWorkshopBo bo) {
        BaseWorkshop update = MapstructUtils.convert(bo, BaseWorkshop.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseWorkshop entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除车间信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
